import os

import requests
from concurrent.futures import ThreadPoolExecutor


def download(image_url):
    res = requests.get(
        url=image_url,
        headers={
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
        }
    )
    return res.content


# 闭包
def outer(file_name):
    def save(response):
        # 检查images目录是否存在？不存在，则创建images目录
        if not os.path.exists('images'):
            # 创建images 目录
            os.makedirs('images')
        file_path = os.path.join('images', file_name)
        # 将图片的内容写入到文件
        with open(file_path, mode='wb') as img_object:
            img_object.write(response.result())
    return save


# 创建线程池，最多维护10个线程
pool = ThreadPoolExecutor(10)

try:
    with open('mv.csv', mode='r', encoding='utf-8') as file_object:
        for line in file_object:
            nid, name, url = line.split(',')
            file_name = "{}.png".format(name)
            future = pool.submit(download, url.strip())
            future.add_done_callback(outer(file_name))
except Exception as e:
    print(e)